package org.foreveross.jbpm.core;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class JoinAssign implements Serializable {
	
	private static final long serialVersionUID = 2974044399273194451L;

	private String name;//会签名
	
	private String keyChoice;
	
	private String type;//会签类型  "NumberJoin" or "PercentJoing"
	
	private String successResult;//会签通过条件
	
	private transient Map<String,String> choiceMap;
	
	private int allCount;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSuccessResult() {
		return successResult;
	}

	public void setSuccessResult(String successResult) {
		this.successResult = successResult;
	}

	public String getKeyChoice() {
		return keyChoice;
	}

	public void setKeyChoice(String keyChoice) {
		this.keyChoice = keyChoice;
	}

	public void addChoice(String user,String value){
		if(choiceMap==null){
			choiceMap = new HashMap<String,String>();
		}
		choiceMap.put(user, value);
	}
	
	public Map<String, String> getChoiceMap() {
		return choiceMap;
	}

	public void setChoiceMap(Map<String, String> choiceMap) {
		this.choiceMap = choiceMap;
	}

	
	public boolean isSuccess(){
	    Collection<String> values =  choiceMap.values();
	    int choice = 0;
	    for(String value:values){
	    	if(value.equals("1")){
	    		choice = choice + 1;
	    	}
	    }
	    if(this.type.equals("NumberJoin") && choice >= Integer.parseInt(successResult)){
	    	return true;
	    }
	    
	    if(type.equals("PercentJoing")){
	    	double percent = (double)choice/this.allCount*100;
	    	if( percent >= Integer.parseInt(successResult)){
	    		return true;
	    	}
	    }
		return false;
	}
	
	public boolean isFinished(){
		if(this.choiceMap.size()==this.allCount)return true;
		return false;
	}
	public int getAllCount() {
		return allCount;
	}

	public void setAllCount(int allCount) {
		this.allCount = allCount;
	}
	
}
